#include<iostream>
#include<string>
#include<vector>
#include<fstream>
#include<algorithm>
#define DDDDD "学生档案.txt"
using namespace std;
//创建学生类
class Student
{
public:
	string m_Name;
	string m_Num;
	string m_Gra;
};

struct
{ 
	bool operator()(Student& s1, Student& s2)
	{
		return s1.m_Gra < s2.m_Gra;
	}
}StoreyBase;
struct
{
	bool operator()(Student& s1, Student& s2)
	{
		return s1.m_Gra > s2.m_Gra;
	}
}StoreySon;

//界面
void face1()
{

	cout << " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
	cout << " *********——学生成绩管理系统——*******" << endl;
	cout << "                                         " << endl;
	cout << " *              1.输入数据              *" << endl;
	cout << " *              2.输出数据              *" << endl;
	cout << " *              3.保存数据              *" << endl;
	cout << " *              4.分数排序              *" << endl;
	cout << " *              5.同步文档              *" << endl;
	cout << " *              6.统计人数              *" << endl;
	cout << " *              7.退出程序              *" << endl;
	cout << " *                                       *" << endl;
	cout << " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
	cout << " 请输入序号选择功能:" << endl;
}

//1.输入数据
void import(vector<Student>& v)
{
	Student s;
	cout << "请依次输入学生姓名、学号、成绩。eg：（小明 2021214 99）" << endl;
	cin >> s.m_Name;
	cin >> s.m_Num;
	cin >> s.m_Gra;
	v.push_back(s);
	system("pause");
	system("cls");
}
//4.排序选择
void choice(int &b)
{   
	cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
	cout << "$  请输入序号选择排序方式  $" << endl;
	cout << "$      1.正序排列          $" << endl;
	cout << "$      2.逆序排列          $" << endl;
	cout << "$      3.取消              $" << endl;
	cout << "****************************" << endl;
	cin >> b;
	if (b == 1 || b == 2)
	{
		cout << "排序成功！" << endl;
		system("pause");
		system("cls");
	}
}

//3.保存数据
void save(vector <Student>& v,ofstream &ofs)
{
	
	ofs.open(DDDDD, ios::out);
	for (vector <Student>::iterator it = v.begin(); it < v.end(); it++)
	{
		ofs << " 姓名： " << (*it).m_Name << "学号： " << (*it).m_Num << "成绩： " << (*it).m_Gra << endl;
	}
	ofs.close();
	cout << "文件已保存至txt文档中" << endl;
	system("pause");
	system("cls");
}
//2.输出数据
void printstudent(vector<Student> v)
{
	for (vector<Student>::const_iterator it = v.begin(); it < v.end(); it++)
	{
		cout <<"姓名: " << (*it).m_Name << " 学号:" << (*it).m_Num << " 分数:" << (*it).m_Gra << endl;
	}
	system("pause");
	system("cls");

}

//5.同步文件
void sync(vector <Student>& v)
{
	ifstream ifs;
	ifs.open(DDDDD, ios::in);
	if (!ifs.is_open())
	{
		cout << "获取文件失败！" << endl;
		return;
	}
	string name;
	string num;
	string gra;
	Student s1;
	//读取到的字符直接放入对应的类对象中会导致乱麻
	// while (ifs >> name && ifs >> num && ifs >> gra)
	// {
	// 	s1.m_Name = name;
	// 	s1.m_Num = num;
	// 	s1.m_Gra = gra;
	// }
	ifstream ifs2;
	vector<double> v2;
	double dou;
	ifs2.open(DDDDD, ios::in);
	if (!ifs2.is_open())
	{
		cout << "获取文件失败！" << endl;
		return;
	}
	while(!ifs2.eof())
	{
      ifs2>>dou;
	  v2.push_back(dou);
	}
	v.push_back(s1);
	ifs.close();
	cout << "文件读取成功！" << endl;
	system("pause");
	system("cls");
}
//6.统计人数
void statistics(vector <Student>& v)
{
	int num=0;
	for (vector<Student>::iterator it = v.begin(); it < v.end(); it++)
	{
		num++;
	}
	cout << "通讯录中共有：" << num << "名同学。" << endl;
	system("pause");
	system("cls");
}
int esc()//7.退出
{
	cout << "欢迎下次使用！" << endl;
	return 0;
}
int main()
{
	ofstream ofs;
	vector <Student> v;
	while (1)
	{
		face1();
		int a;
		cin >> a;
		switch (a)
		{
		case 1:import(v);//输入数据
			break;
		case 2: printstudent(v);//显示数据
			break;
		case 3:save(v, ofs);//保存进文本
			break;
		case 4:int b;//排序，本功能由于封装到函数里会崩，所以直接写出来
			choice(b);
			switch (b)
			{
			case 1:sort(v.begin(), v.end(), StoreyBase);
				break;
			case 2:sort(v.begin(), v.end(), StoreySon);
				break;
		    case 3:break; 
			}
			system("cls");
			break;
		case 5: sync(v);//同步文件
			break;
		case 6:statistics(v);//统计人数
			break;
		case 7: return esc();//退出
		}
    }
}
